const app = getApp();
const log =require('../../log.js');
const util=require('../../util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        count:0,
        total:0,
        number:0,
        timest:0,
        idArray:[],
        timeList:[]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that=this;
        var open=wx.getStorageSync('open');
        that.setData({
            platform:app.globalData.platform,    
            open:open,
            width:app.globalData.windowWidth,
            height:app.globalData.windowHeight,
            statusBarHeight:app.globalData.statusBarHeight,
            navHeight:app.globalData.navHeight,
            marginTop:app.globalData.navTitleTop,
            domain:app.globalData.domain,
            uploadUrl:app.globalData.uploadUrl,
          })
    },
    goBack:function(e){
      console.log(e)
      var pages = getCurrentPages();
      console.log(pages);
      wx.navigateBack();
      },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        //this.getIndex();
        var shopcart = wx.getStorageSync('shopcart');
        var number=0;
        var total= 0.00;
        shopcart.forEach(ele=>{
          number =  parseInt(number) +  parseInt(ele.num);
          console.log('----'+total)
          total = total + parseInt(ele.num)*parseFloat(ele.price);
          console.log('----'+total)
        })
        console.log(total);
        let totals = parseFloat(total).toFixed(2);
        console.log(total);
        this.setData({
          total:totals,
          number:number,
          shopcart:shopcart
        })
    },

    addShopcart(e){
      console.log(e);
      var data = e.currentTarget.dataset;
      var id = data.id;
      var price = data.price;
      var name = data.name;
      var img = data.img;
      var shopcarts =[]
      var shopcart = this.data.shopcart;
      console.log(shopcart);
      var good ={id:id,price:price,num:1,name:name,img:img};
      var number=0;
     var total=0.00;
    // var cates = this.Cates;
    if(shopcart == '' && shopcart == undefined){
      console.log('shopcart is null')
      var shopcarts = [];
      shopcarts.push(good);
      console.log(shopcarts);
      wx.setStorageSync('shopcart', shopcarts)
      this.setData({
        shopcart:shopcarts,
        number:1,
        total:price,
        
      })
     
      }else{
        //console.log('shopcart not null');
        //console.log(shopcart)
        var i=0;
        shopcart.forEach(ele => {
         
           if(ele.id == id){
             
             ele.num=ele.num+1;
             i=1;
             
            // break;
           }
        });
        if(i==0){
           shopcart.push(good);
        }
        shopcart.forEach(ele=>{
           number = number + ele.num;
           total = total + ele.num*ele.price;
        })
        wx.setStorageSync('shopcart', shopcart)
        let totals = parseFloat(total).toFixed(2);
        this.setData({
         shopcart:shopcart,
         number:number,
         total:totals,
         
       })
      
      }
    },
    reduceShopcart(e){
       console.log(e);
       var data = e.currentTarget.dataset;
       var id = data.id;
       var price = data.price;
      // var shopcarts =[]
       var shopcart = this.data.shopcart;
       var index;
       var i=0;
       var number=0;
       var total=0.00;
       shopcart.forEach(ele=>{
         if( ele.id == id){
           if(ele.num>1){
             ele.num = ele.num - 1;
           }else{
            // index = i;
            shopcart.splice(index, 1); 
           }
         }
         i++;
       })
       
       console.log(shopcart);
   
     
       shopcart.forEach(ele=>{
         number = number + ele.num;
         total = total + ele.num*ele.price;
      })
      wx.setStorageSync('shopcart', shopcart)
      let totals = parseFloat(total).toFixed(2);
       this.setData({
         //rightContent:rightContent,
         shopcart:shopcart,
         number:number,
         total:totals,
         
       })
    },
    payment(){
        let that=this;
       let data=this.data;
        var open=wx.getStorageSync('open');
        var money = data.total;
        var number = data.number;
        var domain=data.domain;
        var shopcart = data.shopcart;
        if(money){           
            wx.request({
              url: domain+'wechat/pay/payment',
              data:{open_id:open.id,openid:open.openid,money:money,shopcart:JSON.stringify(shopcart),number:number},
              success: function (res) {
                console.log(res.data);
                 //console.log('调起支付');
                 var code=res.data.code;
                 if(code==200){
                 var data=res.data.data;
                   wx.requestPayment({
                       'timeStamp': data.timeStamp,
                       'nonceStr': data.nonceStr,
                       'package': data.package,
                       'signType': 'MD5',
                       'paySign': data.paySign,
                       'success': function (res) {
                        // console.log('success');
                         wx.showToast({
                           title: '支付成功',
                           icon: 'success',
                           duration: 3000
                         });
                         wx.setStorageSync('shopcart','')
                         setTimeout(function() {
                             //要延时执行的代码
                             wx.reLaunch({
                         
                               url: '../mine/index',
                             })
                           }, 1000) //延迟时间
                         //that.waitPayShow();
                       },
                       'fail': function (res) {
                         //console.log(res);
                       },
                       'complete': function (res) {
                        // console.log('complete');
                       }
                     });
                 }else{
           
                 }
             
               },
               fail: function (res) {
                 //console.log("fail")
               }
            })
        }else{
          console.log('payment')  
        }
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})